﻿@{
    ViewBag.Title = "Common Command";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
        <ol class="breadcrumb">
            <li><a href="/Home/Home">Home</a></li>
              <li class="active">JSON</li>
        </ol>
    
            
            <div id="introduction">
                <h1 class="text-left">Formatters</h1>
                <h4>JsonNetFormatter</h4>
                <p>Json.Net is a popular framework and easily working with the JSON. and it's a more Flexible from the DataContractJsonSerializer.</p>
                <p>JSON(JavaScript Object Notation) is a very popular alternative to XML for transmitting data to the web browser.</p>
                <p>The main important part of the formatter is to provide support for reading and writing content of given media type The sample formmater derives from the MediaTypeFormatter class. here we are working on the buffer media type formatter that will help working with lots of small reads and writes</p>
                <pre><code>
public class JsonNetFormatter : MediaTypeFormatter
{....}
</code></pre>
                <p>here the MediaTypeFormatter is derived from the <b>"System.Net.Formatter.MediaTypeFormatter" after the implement the methods for the formatter at the below side</b></p>
            </div>
            <div id="start">
                <h4>create a constructor in the JsonNetFormmater with the parameter of the JsonSerializerSettings</h4>
                <pre><code>
private readonly JsonSerializerSettings _jsonSerializerSettings;
        
public JsonNetFormatter(JsonSerializerSettings jsonSerializerSettings)
{
    _jsonSerializerSettings = jsonSerializerSettings ?? new JsonSerializerSettings();
    SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
}
</code></pre>
                <p>JsonSerializerSettings is  used to serialize the object. If this is null, default serialization settings will be is used.</p>
                <p>and after the add the SupportedMediaTypes for the Json content</p>
                <p>The following media types are supported by the Web Content Manager REST service</p>
                <ol>
                    <li>
                        <p>Meta Data Media Types</p>
                        <img src="~/Images/Json/JsonMediaType_1.png" alt="Supported Media Type 2" width="100%" height="100%" />
                    </li>
                    <li>
                        <p>Raw Data Media Types</p>
                        <img src="~/Images/Json/JsonMediaType_2.png" alt="Supported Media Type 2" width="100%" height="100%" />
                    </li>
                </ol>
                <p>after override the CanReadType() method derived classes must implement this method and indicate whether a type can be deserialized.</p>
                <p>Queries whether this <b>MediaTypeFormatter</b> can deserializean object of the specified type.</p>
                <pre><code>
public override bool CanReadType(Type type)
{
    return true;
}
</code></pre>
                <p>It's return a true if the <b>MediaTypeFormatter</b> can deserialize the type; otherwise, false.</p>
                <p>after create a Task for the read from the asynchronously from the specified type.</p>
                <pre><code>
public override Task&lt;object&gt; ReadFromStreamAsync(Type type, Stream readStream, System.Net.Http.HttpContent content, IFormatterLogger formatterLogger)
{
    // Create a serializer
    var serializer = JsonSerializer.Create(_jsonSerializerSettings);
    // Create task reading the content
    return Task.Factory.StartNew(() =>
    {
        using (var streamReader = new StreamReader(readStream, Encoding.UTF8))
        {
            using (var jsonTextReader = new JsonTextReader(streamReader))
            {
                return serializer.Deserialize(jsonTextReader, type);
            }
        }
    });
}
</code></pre>
                <p>here, first see to all the parameter of the Task whose repesents the asynchronouse operation that return a value and the <b>Task</b> is defined in the NameSpace of the <b>"System.Threading.Tasks"</b></p>
                <table class="table-condensed table-striped">
                    <tr>
                        <th>Parameters</th>
                        <th>Type</th>
                        <th>Description</th>
                    </tr>
                    <tr>
                        <td>type</td>
                        <td>System.Type</td>
                        <td>The type of the object to deserialize.</td>
                    </tr>
                    <tr>
                        <td>readStream</td>
                        <td>System.IO.Stream</td>
                        <td>The Stream to read.</td>
                    </tr>
                    <tr>
                        <td>content</td>
                        <td>HttpContent</td>
                        <td>The HttpContent, if available. It may be null.</td>
                    </tr>
                    <tr>
                        <td>formatterLogger</td>
                        <td>System.Net.Http.Formatting.IFormatterLogger</td>
                        <td>The IFormatterLogger to log events to.</td>
                    </tr>
                </table>
                <p>
                    Return Value Type is the <b>System.Threading.Tasks.Task&lt;object&gt;</b>
                    and a Task whose result will be an object of the given type.
                </p>
                <p id="JsonCreate">Creates a new <b>JsonSerializer</b> instance using the specified <b>JsonSerializerSettings</b>. The JsonSerializer will not use default settings. and the JsonSerializerSettings will be applied to the JsonSerializer.</p>
                <p><b>Task.Factory.StartNew() </b>was the primary method for scheduling a new task.</p>
                <p><b>StreamReader</b> is Read the text file and it's found in the <b>"System.IO"</b> namespace.</p>
                <p><b>JsonTextReader </b>defined to the <b>"Newtonsoft.Json"</b> namespace and initializes a new instance of the JsonReader class with the specified StreamReader.</p>
                <p>and at the last <b>Deserialize()</b> the text whose methods of the <b>JsonSerializer</b></p>

                <p>after override the CanWriteType() method derived classes must implement this method and indicate whether a type can be serialized.</p>
                <p>Queries whether this <b>MediaTypeFormatter</b> can serializean object of the specified type.</p>
                <pre><code>
public override bool CanWriteType(Type type)
{
    return true;
}
</code></pre>
                <p>It's return a true if the <b>MediaTypeFormatter</b> can serialize the type; otherwise, false.</p>
                <p>after create a Task asynchronously writes an object of the specified type.</p>
                <pre><code>
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, System.Net.Http.HttpContent content, TransportContext transportContext)
{
    // Create a serializer
    var serializer = JsonSerializer.Create(_jsonSerializerSettings);
    object response;
    
    if (value is IWebApiResponse)
    {
        response = value;
    }
    else
    {
        var error = new ErrorResult();
        if (type == typeof (HttpError))
        {
            var httpError = (HttpError) value;
            error.AddError("HttpError", httpError.ExceptionMessage);
        }
        else
        {
            error.AddError("ResponseType", string.Format("Type {0} should impleent IWebApiResponse.", type));
        }
        response = error;
    }
    // Create task writing the serialized content
    return Task.Factory.StartNew(() =>
    {
        using (var streamWriter = new StreamWriter(writeStream, Encoding.UTF8))
        {
            using (var jsonTextWriter = new JsonTextWriter(streamWriter))
            {
                serializer.Serialize(jsonTextWriter, response);
            }
        }
    });
}
</code></pre>
                <p>here, first see to all the parameter of the Task whose repesents the asynchronouse operation that return a value and the <b>Task</b> is defined in the NameSpace of the <b>"System.Threading.Tasks"</b></p>
                <table class="table-condensed table-striped">
                    <tr>
                        <th>Parameters</th>
                        <th>Type</th>
                        <th>Description</th>
                    </tr>
                    <tr>
                        <td>type</td>
                        <td>System.Type</td>
                        <td>The type of the object to write.</td>
                    </tr>
                    <tr>
                        <td>value</td>
                        <td>System.Object</td>
                        <td>The object value to write. It may be null.</td>
                    </tr>
                    <tr>
                        <td>writeStream</td>
                        <td>System.IO.Stream</td>
                        <td>The Stream to which to write.</td>
                    </tr>
                    <tr>
                        <td>content</td>
                        <td>HttpContent</td>
                        <td>The HttpContent if available. It may be null.</td>
                    </tr>
                    <tr>
                        <td>transportContext</td>
                        <td>System.Net.TransportContext</td>
                        <td>The TransportContext if available. It may be null.</td>
                    </tr>
                </table>
                <p>
                    Return Value Type is the <b>System.Threading.Tasks.Task&lt;object&gt;</b>
                    and a Task that will perform the write.
                </p>
                <p><b>JsonSerializer.Create()</b> <a href="#JsonCreate" class="btn-link">click to See</a></p>
                <p><b>object</b> it's the ultimately base class of the all classes in the .Net Framework.It's the root of the type hierarchy</p>
                <p><b>IWebApiResponse</b> is to defined to the Uri and the RedirectToRequired into the Sanelib Library and force all responses to derived from WebApiResonseBase.</p>
                <p>if not to redirect from the <b>IWebApiResponse</b> then the result is false and the return response to the <b>HttpError</b> Exception message.</p>

                <p><b>Task.Factory.StartNew() </b>was the primary method for scheduling a new task.</p>
                <p><b>StreamWriter</b> is Write the text file and it's found in the <b>"System.IO"</b> namespace.</p>
                <p><b>JsonTextWriter </b>defined to the <b>"Newtonsoft.Json"</b> namespace and Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.</p>
                <p>and at the last <b>Deserialize()</b> the text whose methods of the <b>JsonSerializer</b></p>
                <p></p>
            </div>
      